<template>
  <div>
    <el-dialog
        title="修改信息"
        :visible.sync="dialogVisible"
        width="30%"
    >
      <span>
        <el-form ref="form" :model="user" label-width="80px">
          <el-form-item label="请输入原密码">
            <el-input v-model="oldPassword"></el-input>
          </el-form-item>
          <el-form-item label="请输入新密码">
            <el-input v-model="newPassword"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {_studentModify} from '../../../../api/super_admin'

export default {
  name: 'StudentEditDialog',
  data () {
    return {
      dialogVisible: false,
      user: {
        password: '',
        name: '',
        gender: '',
        nation: '',
        account: ''
      },
      oldPassword: '',
      newPassword: ''
    }
  },
  methods: {
    show (Student) {
      this.dialogVisible = true
      this.user = Student
    },
    submit () {
      if (this.oldPassword !== this.user.password) {
        this.$message({
          message: '密码错误',
          type: 'error'
        })
      } else if (this.newPassword === this.oldPassword) {
        this.$message({
          message: '新旧密码一样',
          type: 'error'
        })
      } else if (this.newPassword === '' || this.newPassword === null) {
        this.$message({
          message: '密码不能为空',
          type: 'error'
        })
      } else {
        this.user.password = this.newPassword
        this.$confirm('确定修改密码？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _studentModify(this.user).then(res => {
            if (res.status) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.dialogVisible = false
              this.$emit('ok', this.newPassword)
            } else {
              this.$message({
                message: '修改失败',
                type: 'warning'
              })
            }
          })
        }).catch()
      }
    }
  }
}
</script>

<style scoped>

</style>
